package edu.ccut.saturn.manager.bundles.core;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.List;

import org.osgi.framework.Bundle;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.component.SaturnDataManager;
import edu.ccut.saturn.core.ServiceFactory;

public class QueryAllBundleList extends DefaultComponent {

	private static final String DATA_TYPE = "bundleInfo";
	private static final String NAME_SPACE = "http://edu.ccut.saturn.manager.bundles.core/bundleModule/data";

	@Override
	public void execute(IParameterContext parameterContext) throws Exception {

		String sql = "select id, symbolicName, name, version, status, bundleFilePath , bundleFileName from bundlesInfo";
	
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		try {
			preparedStatement = parameterContext.getConnection()
					.prepareStatement(sql);

			resultSet = preparedStatement.executeQuery();
			List<SaturnData> bundleList = new ArrayList<SaturnData>();
			
			while (resultSet.next()) {
				SaturnData bundleInfo = SaturnDataManager.getInstance()
						.createData(NAME_SPACE, DATA_TYPE);

				bundleInfo.setString("id", resultSet.getString("id"));
				bundleInfo.setString("symbolicName", resultSet.getString("symbolicName"));
				bundleInfo.setString("key", resultSet.getString("name"));
				bundleInfo.setString("version", resultSet.getString("version"));
				bundleInfo.setString("status", resultSet.getString("status"));
				bundleInfo.setString("bundleFilePath", resultSet.getString("bundleFilePath"));
				bundleInfo.setString("bundleFileName", resultSet.getString("bundleFileName"));
				bundleList.add(bundleInfo);
			}
			
			Bundle[] bundles = ServiceFactory.getBundleService().getBundles();
			
			for (Bundle bundle : bundles) {
				String symbolicName = bundle.getSymbolicName();
				Dictionary<?, ?> dictionary = bundle.getHeaders();
				String version = dictionary.get("Bundle-Version").toString();
				String key = dictionary.get("Bundle-Name").toString();
				String status = "enable";
				String url = bundle.getLocation();
				String bundleFileName = "";
				String bundleFilePath = "";
				if (url.indexOf("\\") != -1) {
					bundleFileName = url.substring(url.lastIndexOf("\\") + 1);
					bundleFilePath = url.substring(0, url.lastIndexOf("\\"));
				} else {
					bundleFileName = url;
				}
				SaturnData bundleInfo = SaturnDataManager.getInstance()
						.createData(NAME_SPACE, DATA_TYPE);
				
				bundleInfo.setString("key", key);
				bundleInfo.setString("symbolicName", symbolicName);
				bundleInfo.setString("version", version);
				bundleInfo.setString("status", status);
				bundleInfo.setString("bundleFilePath", bundleFilePath);
				bundleInfo.setString("bundleFileName", bundleFileName);
				bundleList.add(bundleInfo);
			}
			
			parameterContext.setParameter(0, bundleList);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (preparedStatement != null) {
				preparedStatement.close();
			}
			if (resultSet != null) {
				resultSet.close();
			}
		}
	}
}